package com.hualan.service.a_shop;

import com.alibaba.fastjson.JSON;
import com.hualan.entity.Result;
import com.hualan.entity.ResultDate;
import com.hualan.entity.Shop;
import com.hualan.utils.EntityFactory;
import com.hualan.utils.ExecUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@WebServlet(urlPatterns = {"/panghudaojiaBackend/shop/addShop1","/panghudaojiaBackend/shop/modifyShop","/panghudaojiaBackend/shop/querySearchShopList"})
public class ShopService extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String location = req.getServletPath();
        if(location.equals("/panghudaojiaBackend/shop/addShop1")){
            //添加门店
            String sql="insert into a_shop values(null,?,?,?,?,?,?,?,?,?,1,?,0)";
            List<Object> list = new ArrayList<>();
            list.add(req.getParameter("shopName"));
            list.add(req.getParameter("shopDescribe"));
            list.add(req.getParameter("shopPhone"));
            list.add(req.getParameter("shopProvince"));
            list.add(req.getParameter("shopCity"));
            list.add(req.getParameter("shopDistrict"));
            list.add(req.getParameter("shopAddress"));
            list.add(req.getParameter("shopLongitude"));
            list.add(req.getParameter("shopLatitude"));
            LocalDateTime dateTime = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            list.add(dateTime.format(formatter));
            ExecUtil.execute(sql,list,req,true);
            ExecUtil.commit();
            ExecUtil.close();
            resp.getWriter().write(JSON.toJSONString(new Result(200,"添加成功！")));
        }else if(location.equals("/panghudaojiaBackend/shop/modifyShop")){
            //修改门店
            String sql="update a_shop set shopName=?,shopDescribe=?,shopPhone=?,shopProvince=?,shopCity=?,shopDistrict=?,shopAddress=?,shopLongitude=?,shopLatitude=? where shopId=?";
            List<Object> list = new ArrayList<>();
            list.add(req.getParameter("shopName"));
            list.add(req.getParameter("shopDescribe"));
            list.add(req.getParameter("shopPhone"));
            list.add(req.getParameter("shopProvince"));
            list.add(req.getParameter("shopCity"));
            list.add(req.getParameter("shopDistrict"));
            list.add(req.getParameter("shopAddress"));
            list.add(req.getParameter("shopLongitude"));
            list.add(req.getParameter("shopLatitude"));
            list.add(req.getParameter("shopId"));
            ExecUtil.execute(sql,list,req,true);
            ExecUtil.commit();
            ExecUtil.close();
            resp.getWriter().write(JSON.toJSONString(new Result(200,"修改成功！")));
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String location = req.getServletPath();
        if(location.equals("/panghudaojiaBackend/shop/querySearchShopList")){
            //查询门店列表
            List<Object> list = new ArrayList<>();
            String sql = "select * from a_shop where ";
            if(!req.getParameter("phone").equals("")){
                sql += "shopPhone like ? and ";
                list.add("%"+req.getParameter("phone")+"%");
            }
            if(!req.getParameter("keyword").equals("")){
                sql += "shopName like ? and ";
                list.add("%"+req.getParameter("keyword")+"%");
            }
            if(!req.getParameter("shopStatus").equals("")){
                sql += "shopStatus = ? and ";
                list.add(req.getParameter("shopStatus"));
            }
            if(!req.getParameter("startTime").equals("")&&!req.getParameter("endTime").equals("")){
                sql += "addTime between ? and ? and ";
                list.add(req.getParameter("startTime"));
                list.add(req.getParameter("endTime"));
            }
            sql += "\'a\'=\'a\' ";
            ResultSet resultSet1 = (ResultSet) ExecUtil.execute(sql,list,req,false);
            Integer total = 0;
            try{
                while(resultSet1.next()){
                    total++;
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            ExecUtil.commit();
            ExecUtil.close();
            Integer pageNum = Integer.parseInt(req.getParameter("pageNum"));
            Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
            Integer start = pageSize*(pageNum-1);
            Integer end = pageSize*(pageNum);
            sql += "limit "+start+","+end;
            ResultSet resultSet = (ResultSet) ExecUtil.execute(sql,list,req,false);
            List<Object> shops = new ArrayList<>();
            try{
                while (resultSet.next()){
                    EntityFactory entityFactory = new EntityFactory(Shop.class);
                    Shop shop = (Shop) entityFactory.startCombine(resultSet);
                    shops.add(shop);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            ExecUtil.commit();
            ExecUtil.close();
            ResultDate resultDate = new ResultDate(pageNum,pageSize,total,total/pageSize+(total%pageSize == 0 ?0:1),shops);
            Result result = new Result(200,"获取成功",resultDate);
            resp.getWriter().write(JSON.toJSONString(result));
        }
    }
}
